﻿using System.Collections.ObjectModel;
using Eatime.Entitys;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Eatime.Bussiness;
using Eatime.Main.Data;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;

namespace Eatime.Main.DataModel
{
    public class ShopDetailDataSource : Eatime.Main.Common.BindableBase
    {
        public Menu this[int catId] {
            get {
                foreach (var item in Menus)
                {
                    if (item.CatId==catId)
                    {
                        return item;
                    }
                }
                return Menus[0];
            }
        }
        public ShopReviewData ShopReviewDatas { get; set; }
        public List<Item> Items  { get
        {
            if (Menus.Any())
            {
                return Menus[0].Items;
            }
            else
            {
                return new List<Item>();
            }
        } 
        }
        public List<Menu> Menus  { get; set; }
        private ObservableCollection<ShopReviewItem> _shopReviews = new ObservableCollection<ShopReviewItem>();
        public ObservableCollection<ShopReviewItem> ShopReviews { get { return _shopReviews; } }
        public Shop _shop = null;
        public Shop Shop
        {
            get { return _shop; }
            set { _shop = value; }
        }

        private ImageSource _image = null;
        private String _imagePath = null;
        public ImageSource Image
        {
            get
            {
                if (this._image == null && this._imagePath != null)
                {
                    this._image = new BitmapImage(new Uri(_imagePath));
                }
                return this._image;
            }

            set
            {
                this._imagePath = null;
                this.SetProperty(ref this._image, value);
            }
        }

        public void SetImage(String path)
        {
            this._image = null;
            this._imagePath = path;
            this.OnPropertyChanged("Image");
        }
        public ShopDetailDataSource(string shopid,Shop shop)
        {
            this._shop = shop;
            ShopReviewDatas = ShopService.GetShopView(new ShopViewParam(){pageindex = 1,pagesize = 20,shopid = shopid});
            _imagePath = Shop.Logo;
            this.Menus = ShopService.GetShopMenu(new MenuParams() {shopid = int.Parse(shopid)});
        }
    }

    public class ShopReviewItem : Eatime.Main.Common.BindableBase
    {

        public ShopReviewItem(ShopReview shopReview)
        {
            this._shopReviewId = shopReview.ShopReviewId;
            this.Created = shopReview.Created;
            this.DeliverSpeed = shopReview.DeliverSpeed;
            this.ReviewContent = shopReview.ReviewContent;
            this.QoS = shopReview.QoS;
            this._taste = shopReview.Taste;
            this._avgScore = (this.DeliverSpeed + this.QoS + this.Taste)/3;

        } 
        private int _shopReviewId =0;
        public int ShopReviewId
        {
            get { return this._shopReviewId; }
            set { this.SetProperty(ref this._shopReviewId, value); }
        }
         DateTime _created = DateTime.Now;
        public DateTime Created
        {
            get { return this._created; }
            set { this.SetProperty(ref this._created, value); }
        }
         private string _reviewContent =string.Empty;
        public string ReviewContent
        {
            get { return this._reviewContent; }
            set { this.SetProperty(ref this._reviewContent, value); }
        }private int _deliverSpeed = 0;
        public int DeliverSpeed
        {
            get
            {
                return _deliverSpeed;
            }
            set
            {
                this.SetProperty(ref this._deliverSpeed, value);
            }
        }

        private int _taste = 0;
        public int Taste
        {
            get
            {
                return _taste;
            }
            set
            {
                this.SetProperty(ref this._taste, value);
            }
        }

        private int _shopId = 0;
        public int ShopId
        {
            get
            {
                return _shopId;
            }
            set
            {  this.SetProperty(ref this._shopId, value);
            }
        }

        private int _qos = 0;
        /// <summary>
        /// quality of service
        /// </summary>
        public int QoS
        {
            get
            {
                return _qos;
            }
            set
            {
               this.SetProperty(ref this._qos, value);
            }
        }

        private int _avgScore = 0;
        public int AvgScore
        {
            get
            {
                return _avgScore;
            }
            set
            {
               this.SetProperty(ref _avgScore, value);
            }
        }

    }
}
